/*  Copyright (C) 2008  Alexander Blyzniuchenko (BlezAlex)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Windows.Forms;

namespace MediaDC
{
    public partial class MdiWindowsManager : UserControl
    {
        private readonly Form _mainForm;

        public MdiWindowsManager(Form mainForm)
        {
            _mainForm = mainForm;
            InitializeComponent();
        }

        public void AddForm(Form childForm)
        {
            ToolStripButton windowSwitchButton = new ToolStripButton();
            childForm.MdiParent = _mainForm;
            childForm.Tag = windowSwitchButton;
            windowSwitchButton.Tag = childForm;
            windowSwitchButton.Text = childForm.Text;
            windowSwitchButton.Checked = childForm.Visible;
            windowSwitchButton.Image = childForm.Icon.ToBitmap();
            toolStrip1.Items.Add(windowSwitchButton);
            windowSwitchButton.Click += windowSwitchButton_Click;
            childForm.Closed += childForm_Closed;
            childForm.Activated += childForm_Activated;
            childForm.Deactivate += childForm_Deactivate;
            childForm.TextChanged += childForm_TextChanged;
        }

        private static void childForm_TextChanged(object sender, EventArgs e)
        {
            Form senderForm = (Form)sender;
            ((ToolStripButton)((senderForm.Tag))).Text = senderForm.Text;
        }

        private static void childForm_Deactivate(object sender, EventArgs e)
        {
            ((ToolStripButton)(((Form)sender).Tag)).Checked = false;
        }

        private static void childForm_Activated(object sender, EventArgs e)
        {
            ((ToolStripButton)(((Form)sender).Tag)).Checked = true;
        }

        private void childForm_Closed(object sender, EventArgs e)
        {
            Form senderForm = (Form)sender;
            senderForm.Closed -= childForm_Closed;
            senderForm.Activated -= childForm_Activated;
            senderForm.Deactivate -= childForm_Deactivate;
            senderForm.TextChanged -= childForm_TextChanged;
            ToolStripButton button = (ToolStripButton)senderForm.Tag;
            button.Click -= windowSwitchButton_Click;
            toolStrip1.Items.Remove(button);
            senderForm.Dispose();
            button.Dispose();
        }

        private static void windowSwitchButton_Click(object sender, EventArgs e)
        {
            ((Form)((ToolStripButton)sender).Tag).BringToFront();
        }

        private void toolStrip1_SizeChanged(object sender, EventArgs e)
        {
            Height = ((Control)sender).Height;
        }

    }
}